<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
</head>
<body>
  <div id="app">
    <div class="todolist center-block" style="max-width: 600px;margin-top:30px">
      <div class="header row">
        <div class="col-md-10 col-sm-10">
          <input type="text" v-model.trim="inputTxt" class="form-control">
        </div>
        <div class="col-md-2 col-sm-2">
          <button 
            @click="addTodo"
            class="btn btn-primary">增加</button>
        </div>
      </div>
      <div class="lists" style="margin-top: 20px;">
        <ul class="list-group">
          <li 
            v-if="!todos.length"
            class="list-group-item text-center">暂无代办事项</li>
          <li 
            v-for="(todo,index) in todos"
            :key="index"
            class="list-group-item clearfix">
            {{ todo.content }}
              <div class="pull-right">
                <button 
                  @click="changeState(index)"
                  :disabled="todo.isCompleted" class="btn btn-xs btn-success">
                  {{ todo.isCompleted?'已完成':'点击完成' }}
                </button>
                <button 
                  @click="delTodo(index)"
                  class="btn btn-danger btn-xs">删除</button>
              </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        inputTxt: '',
        todos: [
          {
            content:"xxx",
            isCompleted: true
          }
        ]
      },
      methods: {
        addTodo () {
          // 添加代办事项
          if (this.inputTxt !== '') {
            this.todos.push({
              content: this.inputTxt,
              isCompleted: false
            })
            this.inputTxt = '';
          }
        },
        delTodo (index) {
          // 删除代办事项
          if (confirm('您确认删除吗?')) {
            this.todos.splice(index, 1);
          }
        },
        changeState (index) {
          this.todos[index].isCompleted = true;
        }
      }
    })
  </script>
</body>
</html>